package com.shuma.product.service.impl;

import com.shuma.model.product.vo.SellVo;
import com.shuma.model.result.Result;
import com.shuma.model.user.pojo.User;
import com.shuma.product.mapper.OrderMapper;
import com.shuma.product.mapper.ProductMapper;
import com.shuma.rpc.product.StatisticsService;
import com.shuma.utils.UserHolder;
import com.shuma.utils.redis.CacheClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CacheClient cacheClient;
    @Autowired
    private ProductMapper productMapper;
    @Override
    public Result exchange(LocalDate startTime, LocalDate endTime) {
        Long userId = UserHolder.getUser();
        User user = cacheClient.get("user:" + userId.toString(), User.class);
        if (user == null) {
            return Result.error("登陆过期,重新登陆");
        }

        List<Long> productIds = productMapper.selectIdByUserId(userId);
        List<SellVo> sellVos = orderMapper.selectListByProductId(startTime,endTime,productIds);

        return Result.success();
    }
}
